/*
 * é Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

package org.vineatoum.view.action;

import javax.swing.JPanel;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;

import org.vineatoum.business.action.analyse.Prelevement;
import org.vineatoum.business.configuration.MaturityMeasureType;
import org.vineatoum.model.abstraction.IVineaToumModel;

import com.toedter.calendar.JDateChooser;

public class PrelevementPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6779005200522265769L;
	private JDateChooser dateChooserDate;
	private JSpinner spinnerMaturity;
	private JSpinner spinnerAcidity;
	private JDateChooser dateChooserEstimatedDate;
	private JSpinner spinnerEstimatedYield;
	private JTextArea textAreaComment;
	private JComboBox comboBoxMeasurementType;

	/**
	 * Create the panel.
	 * 
	 * @param prelevement
	 * @param model
	 */
	public PrelevementPanel(IVineaToumModel model, Prelevement prelevement) {
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
				1.0, Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		setLayout(gbl_panel);
		{
			JLabel lblDate = new JLabel("Date");
			GridBagConstraints gbc_lblDate = new GridBagConstraints();
			gbc_lblDate.insets = new Insets(0, 0, 5, 5);
			gbc_lblDate.anchor = GridBagConstraints.EAST;
			gbc_lblDate.gridx = 1;
			gbc_lblDate.gridy = 0;
			add(lblDate, gbc_lblDate);
		}
		{
			dateChooserDate = new JDateChooser(prelevement.getDate());
			GridBagConstraints gbc_textField = new GridBagConstraints();
			gbc_textField.insets = new Insets(0, 0, 5, 5);
			gbc_textField.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField.gridx = 2;
			gbc_textField.gridy = 0;
			add(dateChooserDate, gbc_textField);
		}
		{
			JLabel lblType = new JLabel("Type de prélévement");
			GridBagConstraints gbc_lblAlcool = new GridBagConstraints();
			gbc_lblAlcool.anchor = GridBagConstraints.EAST;
			gbc_lblAlcool.insets = new Insets(0, 0, 5, 5);
			gbc_lblAlcool.gridx = 4;
			gbc_lblAlcool.gridy = 0;
			add(lblType, gbc_lblAlcool);
		}
		{
			comboBoxMeasurementType = new JComboBox();
			List<MaturityMeasureType> data = new ArrayList<MaturityMeasureType>();
			for (Iterator<MaturityMeasureType> iterator = MaturityMeasureType.maturityMeasurementTypes
					.iterator(); iterator.hasNext();) {
				MaturityMeasureType it = (MaturityMeasureType) iterator.next();
				if (it.getName() != null && !it.getName().isEmpty()) {
					data.add(it);
				}
			}
			comboBoxMeasurementType.setModel(new DefaultComboBoxModel(data.toArray()));
			comboBoxMeasurementType.setSelectedItem(prelevement.getMeasureType());
			GridBagConstraints gbc_textField_2 = new GridBagConstraints();
			gbc_textField_2.insets = new Insets(0, 0, 5, 5);
			gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_2.gridx = 5;
			gbc_textField_2.gridy = 0;
			add(comboBoxMeasurementType, gbc_textField_2);
		}
		{
			JLabel lblMaturity = new JLabel("Maturité");
			GridBagConstraints gbc_lblTemprature = new GridBagConstraints();
			gbc_lblTemprature.anchor = GridBagConstraints.EAST;
			gbc_lblTemprature.insets = new Insets(0, 0, 5, 5);
			gbc_lblTemprature.gridx = 1;
			gbc_lblTemprature.gridy = 1;
			add(lblMaturity, gbc_lblTemprature);
		}
		{
			spinnerMaturity = new JSpinner();
			spinnerMaturity.setModel(new SpinnerNumberModel(new Float(12),
					new Float(0), new Float(999999), new Float(0.1)));
			spinnerMaturity.setValue(prelevement.getMaturity());
			GridBagConstraints gbc_textField_1 = new GridBagConstraints();
			gbc_textField_1.insets = new Insets(0, 0, 5, 5);
			gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_1.gridx = 2;
			gbc_textField_1.gridy = 1;
			add(spinnerMaturity, gbc_textField_1);
		}
		{
			JLabel lblAlcool = new JLabel("Acidité");
			GridBagConstraints gbc_lblAlcool = new GridBagConstraints();
			gbc_lblAlcool.anchor = GridBagConstraints.EAST;
			gbc_lblAlcool.insets = new Insets(0, 0, 5, 5);
			gbc_lblAlcool.gridx = 4;
			gbc_lblAlcool.gridy = 1;
			add(lblAlcool, gbc_lblAlcool);
		}
		{
			spinnerAcidity = new JSpinner();
			spinnerAcidity.setModel(new SpinnerNumberModel(new Float(5),
					new Float(0), new Float(999999), new Float(0.1)));
			spinnerAcidity.setValue(prelevement.getAcidity());
			GridBagConstraints gbc_textField_2_1 = new GridBagConstraints();
			gbc_textField_2_1.insets = new Insets(0, 0, 5, 5);
			gbc_textField_2_1.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_2_1.gridx = 5;
			gbc_textField_2_1.gridy = 1;
			add(spinnerAcidity, gbc_textField_2_1);
		}
		{
			JLabel lblEstimatedHarvestDate = new JLabel(
					"Date de vendanges estimée");
			GridBagConstraints gbc_lblEstimation = new GridBagConstraints();
			gbc_lblEstimation.anchor = GridBagConstraints.EAST;
			gbc_lblEstimation.insets = new Insets(0, 0, 5, 5);
			gbc_lblEstimation.gridx = 1;
			gbc_lblEstimation.gridy = 2;
			add(lblEstimatedHarvestDate, gbc_lblEstimation);
		}
		{
			dateChooserEstimatedDate = new JDateChooser(prelevement.getEstimatedHarvestDate());
			GridBagConstraints gbc_textField_3 = new GridBagConstraints();
			gbc_textField_3.insets = new Insets(0, 0, 5, 5);
			gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_3.gridx = 2;
			gbc_textField_3.gridy = 2;
			add(dateChooserEstimatedDate, gbc_textField_3);
		}
		{
			JLabel lblEstimatedYield = new JLabel("Redement estimé");
			GridBagConstraints gbc_lblEstimation = new GridBagConstraints();
			gbc_lblEstimation.anchor = GridBagConstraints.EAST;
			gbc_lblEstimation.insets = new Insets(0, 0, 5, 5);
			gbc_lblEstimation.gridx = 4;
			gbc_lblEstimation.gridy = 2;
			add(lblEstimatedYield, gbc_lblEstimation);
		}
		{
			spinnerEstimatedYield = new JSpinner();
			spinnerEstimatedYield.setModel(new SpinnerNumberModel(new Float(50),
					new Float(0), new Float(999999), new Float(5)));
			spinnerEstimatedYield.setValue(prelevement.getEstimatedYield());
			GridBagConstraints gbc_textField_3 = new GridBagConstraints();
			gbc_textField_3.insets = new Insets(0, 0, 5, 5);
			gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_3.gridx = 5;
			gbc_textField_3.gridy = 2;
			add(spinnerEstimatedYield, gbc_textField_3);
		}
		{
			JLabel lblCommentaire = new JLabel("Commentaire");
			GridBagConstraints gbc_lblCommentaire = new GridBagConstraints();
			gbc_lblCommentaire.insets = new Insets(0, 0, 0, 5);
			gbc_lblCommentaire.gridx = 1;
			gbc_lblCommentaire.gridy = 3;
			add(lblCommentaire, gbc_lblCommentaire);
		}
		{
			textAreaComment = new JTextArea();
			textAreaComment.setText(prelevement.getComment());
			textAreaComment.setRows(5);
			GridBagConstraints gbc_textPane = new GridBagConstraints();
			gbc_textPane.insets = new Insets(0, 0, 0, 5);
			gbc_textPane.gridwidth = 4;
			gbc_textPane.fill = GridBagConstraints.BOTH;
			gbc_textPane.gridx = 2;
			gbc_textPane.gridy = 3;
			JScrollPane panel = new JScrollPane();
			panel.setViewportView(textAreaComment);
			add(panel, gbc_textPane);
		}
	}

	/**
	 * @param dateChooserDate the dateChooserDate to set
	 */
	public void setDate(Date date) {
		this.dateChooserDate.setDate(date);
	}

	/**
	 * @return the dateChooserDate
	 */
	public Date getDate() {
		return dateChooserDate.getDate();
	}

	/**
	 * @param spinnerMaturity the spinnerMaturity to set
	 */
	public void setMaturity(float maturity) {
		this.spinnerMaturity.setValue(maturity);
	}

	/**
	 * @return the spinnerMaturity
	 */
	public float getMaturity() {
		return (Float) spinnerMaturity.getValue();
	}

	/**
	 * @param comboBoxMeasurementType the comboBoxMeasurementType to set
	 */
	public void setMeasurementType(MaturityMeasureType measurementType) {
		this.comboBoxMeasurementType.setSelectedItem(measurementType);
	}

	/**
	 * @return the comboBoxMeasurementType
	 */
	public MaturityMeasureType getMeasurementType() {
		return (MaturityMeasureType) comboBoxMeasurementType.getSelectedItem();
	}

	/**
	 * @param spinnerAcidity the spinnerAcidity to set
	 */
	public void setAcidity(float acidity) {
		this.spinnerAcidity.setValue(acidity);
	}

	/**
	 * @return the spinnerAcidity
	 */
	public float getAcidity() {
		return (Float) spinnerAcidity.getValue();
	}

	/**
	 * @param dateChooserEstimatedDate the dateChooserEstimatedDate to set
	 */
	public void setEstimatedDate(Date estimatedDate) {
		this.dateChooserEstimatedDate.setDate(estimatedDate);
	}

	/**
	 * @return the dateChooserEstimatedDate
	 */
	public Date getEstimatedDate() {
		return dateChooserEstimatedDate.getDate();
	}

	/**
	 * @param estimatedYield the spinnerEstimatedYield to set
	 */
	public void setEstimatedYield(float estimatedYield) {
		this.spinnerEstimatedYield.setValue(estimatedYield);
	}

	/**
	 * @return the spinnerEstimatedYield
	 */
	public float getEstimatedYield() {
		return (Float) spinnerEstimatedYield.getValue();
	}

	/**
	 * @param textAreaComment the textAreaComment to set
	 */
	public void setComment(String comment) {
		this.textAreaComment.setText(comment);
	}

	/**
	 * @return the textAreaComment
	 */
	public String getComment() {
		return textAreaComment.getText();
	}

}
